package com.bookmall.cart.listener;

import com.bookmall.cart.domain.OrderInfo;
import com.bookmall.cart.domain.vo.OrderResponseVo;
import com.bookmall.cart.service.IOrderInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Description:
 * @Author 胡菜鸡
 * @Create 2022-04-26-14:02
 */
@RabbitListener(queues = "order.release.order.queue")
@Service
public class OrderCloseListener {
    @Autowired
    private IOrderInfoService orderInfoService;

    /**
     * 监听死信队列
     * @author 胡菜鸡
     * @date 2022/4/26 12:04
     */
    @RabbitHandler
    public void listenerReleaseOrder(OrderResponseVo orderResponseVo, Channel channel, Message message) throws IOException {
        System.out.println("收到过期的订单信息，准备关闭订单：" + orderResponseVo);
        try {
            orderInfoService.closeOrder(orderResponseVo);
            //订单关闭成功，接收消息并将消息移除消息队列
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            //关闭订单失败，拒绝接受消息并将消息重新填入消息队列
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
